import { Component, OnInit, OnDestroy, EventEmitter } from "@angular/core";
import { Observable } from "rxjs/Observable";
import { Subject } from "rxjs/Subject";
import { Subscription } from "rxjs/Subscription";
import { StockEntrustHistory } from "./stock-entrust-history-replay";
import { Headers } from "@angular/http";
import { CmHttp } from "http-module";
import {
  UserToken,
  DEFAULT_VERIFY,
  OPERATE_BUY,
  OPERATE_SELL
} from "vtrade";
import { HsClientService } from "./../hs-client.service";
const TABLE_HEADERS = [
  {
    "label": "证券名称",
    "key": "stock_name"
  },
  {
    "label": "证券代码",
    "key": "stock_code"
  },
  {
    "label": "委托数量",
    "key": "entrust_amount"
  },
  {
    "label": "委托价格",
    "key": "entrust_price"
  },
  {
    "label": "委托方向",
    "key": "entrust_bs"
  },
  {
    "label": "成交数量",
    "key": "business_amount"
  },
  {
    "label": "委托时间",
    "key": "entrust_time"
  },
  {
    "label": "状态",
    "key": "entrust_status",
  },
  {
    "label": "委托编号",
    "key": "entrust_no"
  }
];

@Component({
  // moduleId: module.id,
  selector: "stock-entrust-history",
  templateUrl: "stock-entrust-history.component.html",
  styleUrls: [
    "stock-entrust-history.component.less"
  ],
  inputs: [
    "updateSource"
  ],
  outputs: [
    "positionClick"
  ]
})
export class StockEntrustHistoryComponent implements OnInit, OnDestroy {
  mainSubscription: Subscription;
  tableHeaders = TABLE_HEADERS;
  updateSource: Subject<any>;
  dataSource: Observable<StockEntrustHistory[]>;
  dataList: StockEntrustHistory[];
  positionClick = new EventEmitter();

  constructor(private hsClient: HsClientService) { }

  ngOnInit() {
    !this.updateSource ? this.updateSource = new Subject() : null;
    console.debug("StockPosition UpdateSource: ", this.updateSource);
    this.dataSource =
      this.updateSource
        .switchMap((date) => this.hsClient.queryEntrustHis(date))
    this.mainSubscription = this.dataSource.subscribe(
      res => this.dataList = res,
      err => this.dataList = []
    );
  }

  ngOnDestroy() {
    if (this.mainSubscription) {
      this.mainSubscription.unsubscribe();
    }
  }

  trackBySymbol(symbol: StockEntrustHistory) {
    return symbol.stock_code;
  }

  handleOperate(entry: StockEntrustHistory, isSell?: boolean) {
  }

  getEntryClass(entry: StockEntrustHistory) {
    return {
      "color-red": entry.income_balance >= 0,
      "color-green": entry.income_balance < 0
    };
  }

}